package com.chenlb.mmseg4j.example;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.sf.json.JSONArray;

import com.chenlb.mmseg4j.CharNode;
import com.chenlb.mmseg4j.ComplexSeg;
import com.chenlb.mmseg4j.Dictionary;
import com.chenlb.mmseg4j.Dictionary4ati;
import com.chenlb.mmseg4j.MMSeg;
import com.chenlb.mmseg4j.Seg;
import com.chenlb.mmseg4j.Word;
import com.chenlb.mmseg4j.CharNode.KeyTree;
import com.mijie.fentsi.fentsiController;


public class Complex {

	public Dictionary dic;
	
	/**
	 * o11 
	 * @param n
	 */
	public Complex(int n)
	{
		
	}
	public Complex() {
	 	dic = Dictionary.getInstance();
		
 // 	dic = Dictionary.getInstance("");
//		dic=Dictionary4ati.getInstance("");
		
		
	
		
		
		//delete default words
		Map<Character, CharNode> dict=dic.getDict();		
		Set set=dict.keySet();
		Iterator<Character> it = set.iterator();  
		while (it.hasNext()) {  
			Character key = it.next();  
		//  String val=String.valueOf( dict.get(key));
		  CharNode cn=dict.get(key);
		  cn.ktWordTails=new   KeyTree();		  
		}  
		
		
		
		
		List<String> li=new ArrayList<String>();
		//li.add("上学好");
		//li.add("卖衣裳");
		if(fentsiController.words_include_renvaEventKw!=null)
			li=fentsiController.words_include_renvaEventKw;
		
		for(String word:li)
		{
			if(word.trim().length()==0 || word.length()<2)
				continue;
			String line=word;
			
			CharNode cn = dict.get(line.charAt(0));
			if(cn == null) {
				cn = new CharNode();
				dict.put(line.charAt(0), cn);
			}else
				cn.addWordTail(dic.tail(line));
			
		}
		
//		public void row(String line, int n) {
//			if(line.length() < 2) {
//				return;
//			}
//			CharNode cn = dic.get(line.charAt(0));
//			if(cn == null) {
//				cn = new CharNode();
//				dic.put(line.charAt(0), cn);
//			}
//			cn.addWordTail(tail(line));
//		}
		
	}

	protected Seg getSeg() {
		return new ComplexSeg(dic);
	}
	
	public String segWords(Reader input, String wordSpilt) throws IOException {
		
		StringBuilder sb = new StringBuilder();
		Seg seg = getSeg();	//取得不同的分词具体算法
		MMSeg mmSeg = new MMSeg(input, seg);
		Word word = null;
		boolean first = true;
		while((word=mmSeg.next())!=null) {
			if(!first) {
				sb.append(wordSpilt);
			}
			String w = word.getString();
			sb.append(w);
			first = false;
			li.add(w);
			
		}
		return sb.toString();
	}
	
	public String segWords(String txt, String wordSpilt) throws IOException {
		return segWords(new StringReader(txt), wordSpilt);
	}
	
	private void printlnHelp() {
		System.out.println("\n\t-- 说明: 输入 QUIT 或 EXIT 退出");
        System.out.print("\nmmseg4j-"+this.getClass().getSimpleName().toLowerCase()+">");
	}
	List<String> li=new ArrayList<String>();
	protected void run(String[] args) throws IOException {
		String txt = "京华时报２００８年1月23日报道 昨天，受一股来自中西伯利亚的强冷空气影响，本市出现大风降温天气，白天最高气温只有零下7摄氏度，同时伴有6到7级的偏北风。";
		
		if(args.length > 0) {
			txt = args[0];
		}
		
		String wordSpilt = " | ";
		 segWords(txt, wordSpilt);
		printlnHelp();
		String inputStr = null;
//        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//        while((inputStr = br.readLine()) != null) {
//            if(inputStr.equals("QUIT") || inputStr.equals("EXIT")) {
//            	break;
//               // System.exit(0);
//            } else if("".equals(inputStr)) {
//            	printlnHelp();
//            } else {
//            	//System.out.println(inputStr);
//            	li.add(inputStr);
//            	System.out.println(segWords(inputStr, wordSpilt));    //分词
//            	System.out.print("\nmmseg4j-"+this.getClass().getSimpleName().toLowerCase()+">");
//            }
//        }
	}
	
public List<String>	fentsi(String str)
{
	li=new ArrayList<String>();
		 try {
			run(new String[]{str});
		} catch (IOException e) {
			 
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	    return li;
	 
}
	public static void main(String[] args) throws IOException {
		
		String str = "卖衣裳好还是上学好";
		//new Complex().run(new String[]{str});
		List<String>  arr=	new Complex().fentsi(str);
	 String r = JSONArray.fromObject(arr).toString();
		 System.out.println(r);
	}

}
